package com.example.demo.controller;

import com.example.demo.common.MyTimer;
import com.example.demo.entity.Order;
import com.example.demo.entity.User;
import com.example.demo.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderMapper orderMapper;

    //用户进入选座页面时,为前端返回已经被选的座位号
    @RequestMapping("/get_seat")
    public List<String> seat(String hall, Integer movieId) {
        List<String> list = new ArrayList<>();
        for (Order order :
                orderMapper.seat(hall, movieId)) {
            list.add(order.getSeat());
        }
        return list;
    }

    //根据前端传来的座位号和电影id生成订单插入订单表并且返回电影编号
    @RequestMapping("/seat_confirm")
    public long seat_confirm(Integer id, String[] seats, String hall, HttpServletRequest req) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = dateFormat.format(date);
        long orderNumber = date.getTime();
        HttpSession session = req.getSession(false);
        User user = (User) session.getAttribute("user");
        int userid = user.getUserid();
        for (String seat : seats) {
            orderMapper.seat_selection(id, seat, hall, dateString, orderNumber, userid);
        }
        Timer timer = MyTimer.timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                orderMapper.dele(orderNumber);
            }
        }, 900000);

        return orderNumber;
    }

    //为前端返回该订单编号的数量,确认金额,并且在此页面判断此订单是不是该用户的,和此订单是否已经支付
    @RequestMapping("/getOrderCount")
    public int getOrderCount(Long orderNumber, HttpServletRequest req) throws IOException {
        List<Order> list = orderMapper.ifpay(orderNumber);
        List<Order> list1 = orderMapper.isme(orderNumber);
        HttpSession session = req.getSession(false);
        User user = (User) session.getAttribute("user");
        if (!list1.isEmpty()) {
            Order order = list1.get(0);
            if (order.getUserid() != user.getUserid()) {
                return -2;
            }
        }
        Order order = new Order();
        if (!list.isEmpty()) {
            order = list.get(0);
        } else {
            return 0;
        }
        if (order.getState().equals("1")) {
            return -1;
        }

        return orderMapper.getOrderCount(orderNumber);
    }

    //根据前端传来的电影编号将订单的支付状态该为已支付
    @RequestMapping("/confirmPayment")
    public int confirmPayment(Long orderNumber) {
        List<Order> list = orderMapper.ifpay(orderNumber);
        if (list.isEmpty()) {
            return 0;
        }
        orderMapper.confirmPayment(orderNumber);
        return 1;
    }

    //根据session查询该用户的所有订单信息并将其分类后以最新时间为序列返回给前端
    @RequestMapping("/get_order_list")
    public Object get_order_list(HttpServletRequest req) {
        HttpSession session = req.getSession(false);
        User user = (User) session.getAttribute("user");
        int userid = user.getUserid();
        List<Order> list = orderMapper.get_order_list(userid);
        List<List<Order>> lists = new ArrayList<>();
        while (!list.isEmpty()) {
            List<Order> list1 = new ArrayList<>();
            list1.add(list.get(0));
            list.remove(0);
            Order ordor = list1.get(0);
            for (int i = 0; i < list.size(); i++) {
                Order ordor1 = list.get(i);
                if (ordor1.getOrderNumber().equals(ordor.getOrderNumber())) {
                    list1.add(list.get(i));
                    list.remove(i);
                    i--;
                }
            }
            lists.add(list1);
        }
        Collections.reverse(lists);
        return lists;
    }

    //根据前端给的编号查询此订单的特定信息返回给前端后为用户生成详情订单
    @RequestMapping("/get_detail")
    public List<Order> get_detail(Long orderNumber) {
        return orderMapper.get_detail(orderNumber);
    }
}
